package com.nowcoder.community.controller.admin;

import com.nowcoder.community.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Value("/site/admin/data")
    private String adminPath;

    // 获取管理员页面
    @RequestMapping(path = "/data", method = {RequestMethod.GET, RequestMethod.POST})
    public String getAdminDataPage() {
        return adminPath;
    }

    // 统计网站uv
    @RequestMapping(path = "/data/uv", method = RequestMethod.POST)
    // 使用@DateTimeFormat注解生命客户端传过来的时期的格式
    public String getAdminDataUV(@DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                 Model model) {
        long uv = adminService.calculateUV(startDate, endDate);
        model.addAttribute("uvResult", uv);
        model.addAttribute("uvStartDate", startDate);
        model.addAttribute("uvEndDate", endDate);
        return "forward:/data";
    }

    // 统计网站DAU
    @RequestMapping(path = "/data/dau", method = RequestMethod.POST)
    public String getAdminDataDAU(@DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                 Model model) {
        long dau = adminService.calculateDAU(startDate, endDate);
        model.addAttribute("dauResult", dau);
        model.addAttribute("dauStartDate", startDate);
        model.addAttribute("dauEndDate", endDate);
        return "forward:/data";
    }

}
